<template>
  <div class="audition206">
    <p>lodash的forEach和[].forEach不支持await, forEach 只支持同步代码。</p>
    <p>解决方法1：使用 for...of</p>
    <p>解决方法2：使用 for循环</p>
    <p>解决方法3： 改写forEach，让改写后的forEach支持await</p>
    <v-md-preview :text="text1" />
  </div>
</template>

<script>
const prefix = '``` js'
const suffix = '```'
export default {
  name: 'Audition206',
  data() {
    return {
      text1: `${prefix}
      Array.prototype._foreach = async function (fn, context = null) {
        let index = 0;
        let arr = this;
        if (typeof fn !== 'function') {
          throw new TypeError(fn + ' is not a function');
        }
        while (index < arr.length) {
          if (index in arr) {
            try {
              await fn.call(context, arr[index], index, arr);
            } catch (e) {
              console.log(e);
            }
          }
          index ++;
        }
      }
      \n${suffix}`,
    }
  }
}
</script>

<style>

</style>